package common

const (
	//gocode
	GoCodeServerAddr    = "127.0.0.1:37373"
	GoCodeSockType      = "tcp"
	GoCodeServiceMethod = "Server.AutoComplete"

	//config
	ConfigName        = "conf"
	DefaultConfigPath = "etc/wide_pro.json"

	//auth
	SessionIdName = "id"
	UidName       = "uid"

	//http
	Forbidden = "forbidden"
	//http page
	//LoginPath = "/login"
	AboutPage = "views/about.html"

	//time
	TimeLayout = "2006-01-02 15:04:05"

	//build
	BuildContractSuffix        = ""
	WindowsBuildContractSuffix = ".exe"

	BuildContractName        = "proxyContract"
	WindowsBuildContractName = "proxyContract.exe"

	TemplateDefault = "0"
	//ContentTemplateDefault = "/*\nCopyright (C) BABEC. All rights reserved.\n\nSPDX-License-Identifier: Apache-2.0\n*/\n\npackage main\n\nimport (\n\t\"chainmaker.org/chainmaker/contract-sdk-go/v2/pb/protogo\"\n\t\"chainmaker.org/chainmaker/contract-sdk-go/v2/sandbox\"\n\t\"chainmaker.org/chainmaker/contract-sdk-go/v2/sdk\"\n)\n\n// FactContract contract for fact\ntype FactContract struct {\n}\n\n// InitContract install contract func\nfunc (f *FactContract) InitContract() protogo.Response {\n\treturn sdk.Success([]byte(\"Init contract success\"))\n}\n\n// UpgradeContract upgrade contract func\nfunc (f *FactContract) UpgradeContract() protogo.Response {\n\treturn sdk.Success([]byte(\"Upgrade contract success\"))\n}\n\n// InvokeContract the entry func of invoke contract func\nfunc (f *FactContract) InvokeContract(method string) protogo.Response {\n\tswitch method {\n\tcase \"save\":\n\t\treturn f.save()\n\tcase \"findByFileHash\":\n\t\treturn f.findByFileHash()\n\tdefault:\n\t\treturn sdk.Error(\"invalid method\")\n\t}\n}\n\nfunc (f *FactContract) save() protogo.Response {\n\tparams := sdk.Instance.GetArgs()\n\n\t// 获取参数\n\tfileHash := string(params[\"file_hash\"])\n\n\t// 存储数据\n\terr := sdk.Instance.PutStateByte(\"fact_bytes\", fileHash, []byte(\"1\"))\n\tif err != nil {\n\t\treturn sdk.Error(\"fail to save fact bytes\")\n\t}\n\n\t// 发送事件\n\tsdk.Instance.EmitEvent(\"topic_vx\", []string{fileHash})\n\n\t// 记录日志\n\tsdk.Instance.Infof(\"[save] fileHash=\" + fileHash)\n\n\t// 返回结果\n\treturn sdk.Success([]byte(\"ok\"))\n}\n\nfunc (f *FactContract) findByFileHash() protogo.Response {\n\t// 获取参数\n\tfileHash := string(sdk.Instance.GetArgs()[\"file_hash\"])\n\n\t// 查询结果\n\tresult, err := sdk.Instance.GetStateByte(\"fact_bytes\", fileHash)\n\tif err != nil {\n\t\treturn sdk.Error(\"failed to call get_state\")\n\t}\n\t// 返回结果\n\treturn sdk.Success(result)\n}\n\nfunc main() {\n\terr := sandbox.Start(new(FactContract))\n\tif err != nil {\n\t\tsdk.Instance.Errorf(err.Error())\n\t}\n}\n"
	TemplateCMID1 = "1"

	TemplateCMEVI1 = "2"

	TemplateCMDFA1 = "3"

	TemplateCMNFA1 = "4"

	TemplateEVM = "EVM"
)

var (
	Templates = []TemplateContract{
		{TemplateId: TemplateDefault, ContractName: "空白模板"},
		{TemplateId: TemplateCMDFA1, ContractName: "CMDFA-1"},
		{TemplateId: TemplateCMNFA1, ContractName: "CMNFA-1"},
		{TemplateId: TemplateCMID1, ContractName: "CMID-1"},
		{TemplateId: TemplateCMEVI1, ContractName: "CMEVI-1"},
		{TemplateId: TemplateEVM, ContractName: "EVM"},
	}
)

type TemplateContract struct {
	TemplateId   string `json:"templateId"`
	ContractName string `json:"contractName"`
}

const (
	ErrorInternal = "服务内部错误,稍后重试"
)

const ContentTemplateDefault = `/*
Copyright (C) BABEC. All rights reserved.
Copyright (C) THL A29 Limited, a Tencent company. All rights reserved.

SPDX-License-Identifier: Apache-2.0
*/

package main

import (
	"encoding/json"
	"fmt"
	"log"
	"strconv"

	"chainmaker/pb/protogo"
	"chainmaker/sandbox"
	"chainmaker/sdk"
)

type FactContract struct {
}

// 存证对象
type Fact struct {
	FileHash string 
	FileName string 
	Time     int 
}

// 新建存证对象
func NewFact(fileHash string, fileName string, time int) *Fact {
	fact := &Fact{
		FileHash: fileHash,
		FileName: fileName,
		Time:     time,
	}
	return fact
}

func (f *FactContract) InitContract() protogo.Response {
	return sdk.Success([]byte("Init contract success"))
}

func (f *FactContract) UpgradeContract() protogo.Response {
	return sdk.Success([]byte("Upgrade contract success"))
}

func (f *FactContract) InvokeContract(method string) protogo.Response {
	switch method {
	case "save":
		return f.Save()
	case "findByFileHash":
		return f.FindByFileHash()
	default:
		return sdk.Error("invalid method")
	}
}

func (f *FactContract) Save() protogo.Response {
	params := sdk.Instance.GetArgs()

	// 获取参数
	fileHash := string(params["file_hash"])
	fileName := string(params["file_name"])
	timeStr := string(params["time"])
	time, err := strconv.Atoi(timeStr)
	if err != nil {
		msg := "time is [" + timeStr + "] not int"
		sdk.Instance.Errorf(msg)
		return sdk.Error(msg)
	}

	// 构建结构体
	fact := NewFact(fileHash, fileName, time)

	// 序列化
	factBytes, err := json.Marshal(fact)
	if err != nil {
		return sdk.Error(fmt.Sprintf("marshal fact failed, err: %s", err))
	}
	// 发送事件
	sdk.Instance.EmitEvent("topic_vx", []string{fact.FileHash, fact.FileName})

	// 存储数据
	err = sdk.Instance.PutStateByte("fact_bytes", fact.FileHash, factBytes)
	if err != nil {
		return sdk.Error("fail to save fact bytes")
	}

	// 记录日志
	sdk.Instance.Infof("[save] fileHash=" + fact.FileHash)
	sdk.Instance.Infof("[save] fileName=" + fact.FileName)

	// 返回结果
	return sdk.Success([]byte(fact.FileName + fact.FileHash))

}

func (f *FactContract) FindByFileHash() protogo.Response {
	// 获取参数
	fileHash := string(sdk.Instance.GetArgs()["file_hash"])

	// 查询结果
	result, err := sdk.Instance.GetStateByte("fact_bytes", fileHash)
	if err != nil {
		return sdk.Error("failed to call get_state")
	}

	// 反序列化
	var fact Fact
	if err = json.Unmarshal(result, &fact); err != nil {
		return sdk.Error(fmt.Sprintf("unmarshal fact failed, err: %s", err))
	}

	// 记录日志
	sdk.Instance.Infof("[find_by_file_hash] fileHash=" + fact.FileHash)
	sdk.Instance.Infof("[find_by_file_hash] fileName=" + fact.FileName)

	// 返回结果
	return sdk.Success(result)
}

func main() {
	err := sandbox.Start(new(FactContract))
	if err != nil {
		log.Fatal(err)
	}
}

`
